package com.hui.util;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.hui.mapper.RowMapper;


import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 工具类
 **/
public class DruidUtil {

    static DataSource dataSource = null;

    static {
        InputStream is = DruidUtil.class.getResourceAsStream("/db.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static Connection getConnection() {

        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭资源方法
     **/
    public static void close(Connection conn, Statement stm, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stm != null) {
                stm.close();
            }

            if (conn != null) {
                conn.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 公共修改方法
     **/
    public static void commonUpdate(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            conn = DruidUtil.getConnection();

            pstm = conn.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i + 1, args[i]);
            }

            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(conn, pstm, null);
        }

    }

    /**
     * 公共查询对象方法
     **/
    public static <T> T commonFindObjectByParam(String sql, RowMapper<T> mapper, Object... args) {

        T t = null;

        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            //创建连接
            conn = DruidUtil.getConnection();

            //创建代码块
            pstm = conn.prepareStatement(sql);

            //为占位符赋值
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i + 1, args[i]);
            }

            //执行sql
            rs = pstm.executeQuery();
            while (rs.next()) {
                //把一条查询的数据封装成一个对象
                t = mapper.rowToEntity(rs);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(conn, pstm, rs);
        }

        return t;
    }


    /**
     * 公共查询对象集合方法
     **/
    public static <T> List<T> commonFindListByParam(String sql, RowMapper<T> mapper) {

        List<T> list = new ArrayList<>();

        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            //创建连接
            conn = DruidUtil.getConnection();


            //创建代码块
            pstm = conn.prepareStatement(sql);

            //执行sql
            rs = pstm.executeQuery();

            while (rs.next()) {
                //   如果把一条查询到的数据，封装成一个t对象  rs
                T t = mapper.rowToEntity(rs);
                list.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            DruidUtil.close(conn, pstm, rs);
        }
        return list;
    }


    /**
     * 添加数据，获取主键id
     **/
    public static int commonUpdateReturnKey(String sql, Object... args) {
        int id = 0;
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            //创建连接
            conn = DruidUtil.getConnection();

            //创建代码块
            pstm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

            //为占位符赋值
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i + 1, args[i]);
            }
            //执行sql
            pstm.executeUpdate();

            //获取主键
            rs = pstm.getGeneratedKeys();

//            rs.next(); // 获取第一行
            while (rs.next()) {
                id = rs.getInt(1); //第一列
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(conn, pstm, rs);
        }
        return id;
    }

}
